package ifips.s7.modele;

import java.util.ArrayList;
import java.util.Iterator;

public class Turing {

	  
	  protected String lettre_vide; 
	  ArrayList<Etat_Turing> Etats_Turing;
	  
	    public Turing(String vide){
	        this.lettre_vide = vide;
	        Etats_Turing = new ArrayList<Etat_Turing>();
	    }
	    
	    public void addEtat( Etat_Turing E){
	        this.Etats_Turing.add(E);
	    }
	    public void addTransition(Etat_Turing E1, String l1, boolean dir, String l2, Etat_Turing E2){
	        Transition_turing T = new Transition_turing(E1,l1,dir,l2,E2);
	        T.getDepart().addTransition(T);
	    }

	    
	    public Etat_Turing getEtat( int id){
	      
	    	for (Iterator it = Etats_Turing.iterator(); it.hasNext();){


	    	      	Etat_Turing E = (Etat_Turing) it.next();

	    	      	if(E.getId()==id) return E;
	    		}
	        	return null;

	    }
	    
	    
	    public boolean isReadable(String mot){
                    System.out.println();
                    System.out.println("Test de lecture du mot "+mot);
                    for (Etat_Turing i : Etats_Turing){
                            if (i.Initial) {
                                if (lastEtat(i,mot,0,0)) return true;
                            }
                    }
                    return false;
	    }
	    
	    String change(String mot,int indice,String lettre){
	    	if(lettre==null) return mot;
	    	StringBuilder new_mot = new StringBuilder(mot);

	    	new_mot.setCharAt(indice,lettre.charAt(0));
	    	
	    	return new_mot.toString();
	    }


	    public boolean lastEtat(Etat_Turing E, String mot,int bande,int iteration){
	         for (Transition_turing j : E.getTransitions()){
	                if (j.lettre.equals(String.valueOf(mot.charAt(bande))) && iteration<100){	                	
	                    mot=change(mot,bande,j.lettre2);
	                    if(j.getDirection() && bande==mot.length()-1){ 
                    		mot = mot.concat(lettre_vide);
                    		bande++;
	                    }
	                    else if(j.getDirection()){
                    		bande++;
	                    }
	                    else if(!j.getDirection() && bande==0){
	                    	mot=lettre_vide.concat(mot);
	                    }
	                    else if(!j.getDirection()){
	                    	bande--;
	                    }
	                    if(lastEtat(j.getArrivee(),mot,bande,iteration+1)) return true;
	                }
	         }
	         if(E.isFinal())return true;
	         if(iteration==10000)System.out.println("boucle infinie");
	         return false;
	     }
	    
        public void print(){

     		for (Iterator it = Etats_Turing.iterator(); it.hasNext();){


    			Etat_Turing E = (Etat_Turing) it.next();
    			String direction;

    			System.out.println("Etat: "+E.getId());
                            System.out.println("\t Transitions: ");

                                 for (Iterator it2 = E.getTransitions().iterator(); it2.hasNext();){

                                     Transition_turing T = (Transition_turing) it2.next();
                                     if(T.getDirection())direction="Droite";
                                     else direction = "Gauche";
                                     if(T.getnvleLettre()!=null)System.out.println("\t lettre: "+T.getLettre()+" direction: "+direction+" en changeant en: "+T.getnvleLettre()+" vers: "+T.getArrivee().getId());
                                     else System.out.println("\t lettre: "+T.getLettre()+" direction: "+direction+" sans changer la lettre vers: "+T.getArrivee().getId());
                        }	

                            
    		}

       }
        public String whatisread(Etat_Turing E,String mot,int bande){
                System.out.println();
                System.out.println("Test de lecture du mot "+mot);
                if(bande==0)
                for (Transition_turing j : E.getTransitions()){

                        if (j.lettre.equals(String.valueOf(mot.charAt(bande)))){
                            mot=change(mot,bande,j.lettre2);
                            if(j.getDirection() && bande==mot.length()-1){
                                mot = mot.concat(lettre_vide);
                                bande++;
                            }
                            else if(j.getDirection()){
                                bande++;
                            }
                            else if(!j.getDirection() && bande==0){
                                mot=lettre_vide.concat(mot);
                            }
                            else if(!j.getDirection()){
                                bande--;
                            }
                            return whatisread(j.getArrivee(),mot,bande);
                        }
                 }
                 return mot;
        }



        public String whatdoyouread(String mot){
                for (Etat_Turing i : Etats_Turing){
                    if (i.Initial) {
                       return whatisread(i,mot,0);
                    }
                }               
                return null;
        }
	    
}
